package com.fesine.handle;

import com.fesine.entity.Result;
import com.fesine.exception.UserException;
import com.fesine.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description: 统一异常处理
 * 1、使用@ControllerAdvice注解
 * 2、在方法上添加@ResponseBody注解，返回json格式
 * 3、@ExceptionHandler(value = Exception.class)注解，处理Exception异常
 * @author: Fesine
 * @createTime:2017/8/30 21:32
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/8/30 21:32
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result handle(Exception e){
        if (e instanceof UserException) {
            UserException uE = (UserException) e;
            return ResultUtil.error(uE.getCode(), uE.getMessage());
        }
        logger.error("系统异常 {}",e);
        return ResultUtil.error(-1, "未知异常！");

    }

}
